using System;
using Asteroid.Content;
using GameFramework.GameObjects;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace Asteroid.GameObjects
{
    /// <summary>
    /// This class represent the spaceship object.
    /// </summary>
    public class Spaceship : Texture
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Spaceship"/> class.
        /// </summary>
        public Spaceship()
        {
            TextureName = Textures.Spaceship;
            SetOriginAtCenter();
        }

        /// <summary>
        /// Update the spaceship.
        /// </summary>
        public override void Update()
        {
            base.Update();

            //// Move the spaceship using the accelerometer or the keyboard if we are running in the emulator

            float dx = 0;

            if (Game.IsRunningInEmulator)
            {
                KeyboardState keyboardState = Keyboard.GetState();

                if (keyboardState.IsKeyDown(Keys.Left))
                {
                    dx = -0.3f;
                }

                if (keyboardState.IsKeyDown(Keys.Right))
                {
                    dx = 0.3f;
                }
            }
            else
            {
                dx = Game.Input.AccelerometerData.X;                
            }

            // Move the spaceship only if the magnitude of the movement is higher than a specific tolerance
            if (Math.Abs(dx) > 0.05f)
            {
                PositionX += 100 * dx;

                // Force the spaceship to remain inside the screen
                PositionX = MathHelper.Clamp(PositionX, 50, Game.ScreenWidth - 50);
            }
        }
    }
}
